import {
  customerReceiptConfirm,
  detectCreate,
  detectResultSave,
  getCustomerReceiptAddress,
  getDetectDetail,
  getDetectDetailCustomField,
  getDetectGoodList,
  getDetectList,
  getInfoBeforeUpdateReturnFactory,
  getLogisticsOptions,
  getReceiptAddress,
  getReturnFactoryDetail,
  getReturnFactoryDetailCustomField,
  getReturnFactoryDetailGoodList,
  getReturnFactoryDetailOperateLog,
  getReturnFactoryGoodDetailById,
  getReturnFactoryList,
  getReturnFactoryModuleCheckSetById,
  getReturnFactoryModuleCustomFieldById,
  getReturnFactoryReceiptProofreadingSet,
  getReturnFactoryWaitCheckGoods,
  returnFactoryCheck,
  returnFactoryCreate,
  returnFactoryReceiptGoodsSave,
  returnLogisticsFill,
  saveReturnFactoryLogisticsInfo,
  updateReturnFactoryGoodInfo,
} from '@/api/return-factory'
import { ref } from 'vue'

export const useReturnFactoryForm = () => {
  const loading = ref(false)
  const form = ref<ReturnFactoryAPIRequest.Create>({
    address: '',
    area: '',
    city: '',
    faultDescr: '',
    fieldValVos: [],
    otherPhone: '',
    phone: '',
    province: '',
    repairDescr: '',
    repairGoodsVos: [],
    createTime: '',
    repairModuleId: '',
    repairPaths: [],
    source: 1,
    userId: '',
    username: '',
    receiveAddrVo: {
      customerReceiveAddress: '',
      customerReceiveArea: '',
      customerReceiveCity: '',
      customerReceivePhone: '',
      customerReceiveProvince: '',
      customerReceiveUsername: '',
      customerTakePieceMode: 2,
    },
    networkId: '',
  })

  const create = async () => {
    loading.value = true
    const res = await returnFactoryCreate(form.value)
    loading.value = false
    return res.data
  }

  const resetForm = () => {
    form.value = {
      address: '',
      area: '',
      city: '',
      faultDescr: '',
      fieldValVos: [],
      otherPhone: '',
      phone: '',
      province: '',
      createTime: '',
      repairDescr: '',
      repairGoodsVos: [],
      repairModuleId: '',
      repairPaths: [],
      source: 1,
      userId: '',
      username: '',
      receiveAddrVo: {
        customerReceiveAddress: '',
        customerReceiveArea: '',
        customerReceiveCity: '',
        customerReceivePhone: '',
        customerReceiveProvince: '',
        customerReceiveUsername: '',
        customerTakePieceMode: 2,
      },
      networkId: '',
    }
  }

  return {
    form,
    loading,
    create,
    resetForm,
  }
}

/**
 * 获取新增时的自定义字段
 */
export const useGetCustomFieldForCreate = () => {
  // 获取自定义字段加载
  const loading = ref(false)

  // 自定义字段
  const customFieldList = ref<Array<ReturnFactoryCustomFieldForCreate>>([])

  // 获取自定义字段方法
  const getCustomField = async (moduleId: string) => {
    loading.value = true
    const res = await getReturnFactoryModuleCustomFieldById(moduleId)
    loading.value = false
    customFieldList.value = res.data.data.rows
  }

  return {
    loading,
    customFieldList,
    getCustomField,
  }
}

/**
 * 获取返厂单列表
 */
export const useGetReturnFactoryList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 获取列表请求参数
  const getListParam = ref<ReturnFactoryAPIRequest.GetList>({
    limit: 50,
    page: 1,
    repairModuleId: '',
    repairNo: '',
    status: '',
    userKeyword: '',
  })

  // 列表
  const list = ref<Array<ReturnFactoryAPIResponse.RepairListItem>>([])

  // 列表总条数
  const total = ref(0)

  /**
   * 获取列表
   */
  const getList = async () => {
    getListLoading.value = true
    const res = await getReturnFactoryList(getListParam.value)
    list.value = res.data.data.rows
    total.value = Number(res.data.data.total)
    getListLoading.value = false
  }

  /**
   * 清空所有的搜索条件
   */
  const clearGetListParam = () => {
    getListParam.value = {
      limit: 50,
      page: 1,
      repairModuleId: '',
      repairNo: '',
      status: '',
      userKeyword: '',
    }
  }

  return {
    list,
    total,
    getListParam,
    getListLoading,
    getList,
    clearGetListParam,
  }
}

/**
 * 获取返厂详情
 */
export const useGetReturnFactoryDetail = () => {
  // 获取详情加载
  const getDetailLoading = ref(false)

  // 返厂详情
  const detail = ref<ReturnFactoryDetail>({
    address: '',
    area: '',
    city: '',
    createTime: '',
    faultDescr: '',
    id: '',
    otherPhone: '',
    phone: '',
    province: '',
    receiveAddrVo: {
      customerReceiveAddress: '',
      customerReceiveArea: '',
      customerReceiveCity: '',
      customerReceivePhone: '',
      customerReceiveProvince: '',
      customerReceiveUsername: '',
      customerTakePieceMode: 2,
    },
    repairDescr: '',
    repairModule: '',
    repairNo: '',
    source: 1,
    status: 1,
    town: '',
    updateTime: '',
    username: '',
    emp: '',
    images: [],
    contacts: '',
    enableNetwork: false,
    network: '',
    btnEventType: 0,
  })

  /**
   * 获取详情
   * @param {String} id 返厂id
   */
  const getDetail = async (id: string) => {
    getDetailLoading.value = true
    const res = await getReturnFactoryDetail(id)
    detail.value = res.data.data.row
    getDetailLoading.value = false
  }

  return {
    detail,
    getDetailLoading,
    getDetail,
  }
}

/**
 * 根据返厂id获取详情的自定义字段
 */
export const useGetDetailCustomFieldById = () => {
  // 加载
  const loading = ref(false)

  // 自定义字段列表
  const customFieldList = ref<Array<ReturnFactoryDetailCustomField>>([])

  /**
   * 获取自定义字段
   * @param {String} id - 返厂id
   */
  const getCustomField = async (id: string) => {
    loading.value = true
    const res = await getReturnFactoryDetailCustomField(id)
    customFieldList.value = res.data.data.row
    loading.value = false
  }

  return {
    loading,
    customFieldList,
    getCustomField,
  }
}

/**
 * 获取返厂详情的操作日志
 */
export const useGetDetailOperateLog = () => {
  // 获取操作日志加载
  const loading = ref(false)

  // 获取操作日志请求参数
  const getOperateLogParam = ref({
    limit: 50,
    page: 1,
  })

  // 当前页码
  const currentPage = ref(1)

  // 操作日志列表
  const operateLogList = ref<Array<ReturnFactoryDetailOperateLog>>([])

  // 列表总条数
  const total = ref(0)

  /**
   * 获取操作日志
   * @param {String} id - 返厂id
   */
  const getOperateLog = async (id: string) => {
    loading.value = true
    const res = await getReturnFactoryDetailOperateLog(id, getOperateLogParam.value)
    operateLogList.value = res.data.data.rows
    total.value = Number(res.data.data.total)
    currentPage.value = Number(res.data.data.currentPage)
    loading.value = false
  }

  return {
    total,
    loading,
    currentPage,
    operateLogList,
    getOperateLogParam,
    getOperateLog,
  }
}

/**
 * 获取返厂详情的产品清单
 */
export const useGetReturnFactoryDetailGoodList = () => {
  // 获取产品清单加载
  const loading = ref(false)

  // 获取产品清单请求参数
  const getGoodListParam = ref({
    limit: 100,
    page: 1,
  })

  // 列表总条数
  const total = ref(0)

  // 产品清单
  const goodList = ref<Array<ReturnFactoryDetailGood>>([])

  /**
   * 获取产品清单
   * @param {String} id - 返厂id
   */
  const getGoodList = async (id: string) => {
    loading.value = true
    const res = await getReturnFactoryDetailGoodList(id, getGoodListParam.value)
    goodList.value = res.data.data.rows
    total.value = Number(res.data.data.total)
    loading.value = false
  }

  return {
    total,
    loading,
    goodList,
    getGoodListParam,
    getGoodList,
  }
}

/**
 * 获取修改返厂前的信息
 */
export const useGetInfoBeforeUpdateReturnFactory = () => {
  // 获取信息加载
  const loading = ref(false)

  /**
   * 获取信息
   * @param {String} id - 返厂id
   */
  const getInfo = async (id: string) => {
    loading.value = true
    await getInfoBeforeUpdateReturnFactory(id)
    loading.value = false
  }

  return {
    loading,
    getInfo,
  }
}

/**
 * 获取返厂模块审核设置
 */
export const useGetReturnFactoryModuleCheckSet = () => {
  // 获取设置加载
  const loading = ref(false)

  // 返厂审核设置
  const returnFactoryCheckSet = ref<1 | 2>(1)

  /**
   * 获取设置
   * @param {String} id - 返厂id
   */
  const getSet = async (id: string) => {
    loading.value = true
    const res = await getReturnFactoryModuleCheckSetById(id)
    returnFactoryCheckSet.value = res.data.data.row.repairCheckType
    loading.value = false
  }

  return {
    loading,
    returnFactoryCheckSet,
    getSet,
  }
}

/**
 * 获取返厂待审核产品列表
 */
export const useGetReturnFactoryWaitCheckGoodList = () => {
  // 获取待审核产品列表加载
  const loading = ref(false)

  // 待审核产品列表
  const waitGoodList = ref<Array<ReturnFactoryWaitCheckGood>>([])

  /**
   * 获取待审核产品列表
   * @param {String} id - 返厂id
   */
  const getWaitCheckGoodList = async (id: string) => {
    loading.value = true
    const res = await getReturnFactoryWaitCheckGoods(id)
    waitGoodList.value = res.data.data.row
    loading.value = false
  }

  return {
    loading,
    waitGoodList,
    getWaitCheckGoodList,
  }
}

/**
 * 返厂审核
 */
export const useReturnFactoryCheck = () => {
  // 审核加载
  const checkLoading = ref(false)

  // 返厂审核表单
  const form = ref<ReturnFactoryAPIRequest.ReturnFactoryCheckForm>({
    checkStatus: 1,
    handleMode: 1,
    handleOpinion: '',
    repariGoodsIds: [],
    repairGoodsPrice: [],
  })

  /**
   * 审核
   * @param {String} id - 返厂id
   */
  const check = async (id: string) => {
    checkLoading.value = true
    const res = await returnFactoryCheck(id, form.value)
    checkLoading.value = false
    return res.data
  }

  return {
    form,
    checkLoading,
    check,
  }
}

/**
 * 返厂物流信息保存
 */
export const useReturnFactoryLogisticsInfoSave = () => {
  // 保存加载
  const loading = ref(false)

  // 表单
  const form = ref<ReturnFactoryAPIRequest.ReturnFactoryLogisticsInfoSave>({
    receiveAddress: '',
    receiveArea: '',
    receiveCity: '',
    receivePhone: '',
    receiveProvince: '',
    receiveUsername: '',
    repairGoodsLogisticsVos: [],
  })

  /**
   * 保存方法
   * @param {String} id - 返厂id
   */
  const save = async (id: string) => {
    loading.value = true
    const res = await saveReturnFactoryLogisticsInfo(id, form.value)
    loading.value = false
    return res.data
  }

  return {
    form,
    loading,
    save,
  }
}

/**
 * 获取物流下拉选项
 */
export const useGetLogisticsOptions = () => {
  // 获取下拉选项加载
  const loading = ref(false)

  // 获取下拉选项请求参数
  const getOptionsParam = ref({
    name: '',
    limit: 50,
    page: 1,
  })

  // 物流下拉选项
  const options = ref<Array<{ code: string; name: string }>>([])

  /**
   * 获取下拉选项
   */
  const getOptions = async () => {
    loading.value = true
    const res = await getLogisticsOptions(getOptionsParam.value)
    options.value = res.data.data.rows
    loading.value = false
  }

  return {
    options,
    loading,
    getOptionsParam,
    getOptions,
  }
}

/**
 * 根据返厂详情的产品清单id获取产品详情
 */
export const useGetGoodDetail = () => {
  // 获取产品详情加载
  const loading = ref(false)

  // 产品详情
  const goodDetail = ref<ReturnFactoryGoodDetail>({
    address: '',
    area: '',
    city: '',
    code: '',
    descr: '',
    goodsBrand: '',
    goodsCode: '',
    goodsModel: '',
    goodsName: '',
    goodsSpec: '',
    handleMode: 0,
    handleOpinion: null,
    id: '',
    images: [],
    number: '',
    province: '',
    qty: 1,
    registerTime: null,
    repairCheckNo: null,
    sendBackLogisticsName: '',
    sendBackLogisticsNo: '',
    sendOutLogisticsName: '',
    sendOutLogisticsNo: '',
    status: 0,
    userPhone: '',
    username: '',
    warrantyEndTime: '',
    warrantyStartTime: '',
    type: 1,
  })

  /**
   * 获取产品详情
   * @param {String} id - 产品清单id
   */
  const getGoodDetail = async (id: string) => {
    loading.value = true
    const res = await getReturnFactoryGoodDetailById(id)
    goodDetail.value = res.data.data.row
    loading.value = false
  }

  return {
    loading,
    goodDetail,
    getGoodDetail,
  }
}

/**
 * 获取返厂签收设置
 */
export const useGetReturnFactoryReceiptSet = () => {
  // 获取设置加载
  const loading = ref(false)

  // 签收设置
  const receiptSet = ref(false)

  /**
   * 获取设置
   * @param {String} id - 返厂id
   */
  const getSet = async (id: string) => {
    loading.value = true
    const res = await getReturnFactoryReceiptProofreadingSet(id)
    loading.value = false
    receiptSet.value = res.data.data.row.enabledSnCodeCheck
  }

  return {
    loading,
    receiptSet,
    getSet,
  }
}

/**
 * 保存返厂签收产品
 */
export const useReturnFactoryReceiptGoodsSave = () => {
  // 保存加载
  const loading = ref(false)

  // 产品列表
  const goods = ref<Array<ReturnFactoryAPIRequest.ReturnFactoryReceiptGoodSave>>([])

  /**
   * 保存
   * @param {String} id - 返厂id
   */
  const save = async (id: string) => {
    loading.value = true
    const res = await returnFactoryReceiptGoodsSave(id, goods.value)
    loading.value = false
    return res.data
  }

  return {
    goods,
    loading,
    save,
  }
}

/**
 * 更新返厂产品
 */
export const useUpdateReturnFactoryGoodInfo = () => {
  // 更新加载
  const loading = ref(false)

  // 表单
  const form = ref<{ images: Array<string>; repairDescr: string }>({
    images: [],
    repairDescr: '',
  })

  /**
   * 更新返厂的产品信息
   * @param {String} id - 返厂id
   */
  const update = async (id: string) => {
    loading.value = true
    const res = await updateReturnFactoryGoodInfo(id, form.value)
    loading.value = false
    return res.data
  }

  return {
    form,
    loading,
    update,
  }
}

/**
 * 创建检测单
 */
export const useDetectCreate = () => {
  // 创建加载
  const createLoading = ref(false)

  // 创建表单
  const form = ref<ReturnFactoryAPIRequest.DetectCreate>({
    fieldValVos: [],
    repairGoodsIds: [],
    repairId: '',
  })

  /**
   * 创建方法
   */
  const create = async () => {
    createLoading.value = true
    const res = await detectCreate(form.value)
    createLoading.value = false
    return res.data
  }

  return {
    form,
    createLoading,
    create,
  }
}

/**
 * 获取检测单列表
 */
export const useGetDetectList = () => {
  // 获取检测单列表加载
  const getListLoading = ref(false)

  // 检测列表
  const list = ref<Array<ReturnFactoryDetectItem>>([])

  // 获取列表请求参数
  const getListParam = ref<ReturnFactoryAPIRequest.GetDetectList>({
    limit: 50,
    page: 1,
    userSearch: '',
    repairCheckNo: '',
  })

  // 列表总条数
  const total = ref(0)

  /**
   * 获取列表
   */
  const getList = async () => {
    getListLoading.value = true
    const res = await getDetectList(getListParam.value)
    total.value = Number(res.data.data.total)
    list.value = res.data.data.rows
    getListLoading.value = false
  }

  return {
    list,
    total,
    getListParam,
    getListLoading,
    getList,
  }
}

/**
 * 获取检测单详情
 */
export const useGetDetectDetail = () => {
  // 获取详情加载
  const getDetailLoading = ref(false)

  // 检测单详情
  const detail = ref<ReturnFactoryDetectDetail>({
    address: ' ',
    area: '',
    city: '',
    createTime: '',
    id: '',
    phone: '',
    province: '',
    repairCheckNetwork: '',
    repairCheckNetworkPhone: '',
    repairCheckNetworkUsername: '',
    repairCheckNo: '',
    repairNo: '',
    udpateTime: '',
    username: '',
    status: 0,
    repairId: '',
  })

  /**
   * 获取详情
   * @param {String} id - 检测单id
   */
  const getDetail = async (id: string) => {
    getDetailLoading.value = true
    const res = await getDetectDetail(id)
    detail.value = res.data.data.row
    getDetailLoading.value = false
  }

  return {
    detail,
    getDetailLoading,
    getDetail,
  }
}

/**
 * 获取检测单自定义字段
 */
export const useGetDetectCustomField = () => {
  // 获取字段加载
  const getFieldLoading = ref(false)

  /**
   * 获取字段
   * @param {String} id - 检测单id
   */
  const getField = async (id: string) => {
    getFieldLoading.value = true
    const res = await getDetectDetailCustomField(id)
    getFieldLoading.value = false
  }

  return {
    getFieldLoading,
    getField,
  }
}

/**
 * 获取检测清单
 */
export const useGetDetectGoodList = () => {
  // 获取清单加载
  const getGoodListLoading = ref(false)

  // 检测清单
  const goodList = ref<Array<ReturnFactoryDetectGood>>([])

  /**
   * 获取清单
   * @param {String} id - 检测单id
   */
  const getGoodList = async (id: string) => {
    getGoodListLoading.value = true
    const res = await getDetectGoodList(id)
    goodList.value = res.data.data.rows
    getGoodListLoading.value = false
  }

  return {
    goodList,
    getGoodListLoading,
    getGoodList,
  }
}

/**
 * 保存检测结果
 */
export const useDetectResultSave = () => {
  // 保存加载
  const saveLoading = ref(false)

  // 保存请求参数
  const saveParam = ref<Array<ReturnFactoryAPIRequest.DetectResultSave>>([])

  /**
   * 保存事件
   * @param {String} id - 检测单id
   */
  const save = async (id: string) => {
    saveLoading.value = true
    const res = await detectResultSave(saveParam.value, id)
    saveLoading.value = false
    return res.data
  }

  return {
    saveParam,
    saveLoading,
    save,
  }
}

/**
 * 获取公司收货地址
 */
export const useGetCustomerReceiptAddress = () => {
  // 获取收货地址加载
  const getReceiptAddressLoading = ref(false)

  // 公司收货地址
  const receiptAddress = ref<ReturnFactoryCustomerReceiptAddress>({
    customerReceiveAddress: '',
    customerReceiveArea: '',
    customerReceiveCity: '',
    customerReceivePhone: '',
    customerReceiveProvince: '',
    customerReceiveTown: '',
    customerReceiveUsername: '',
    id: '',
  })

  /**
   * 获取收货地址
   * @param {String} id - 返厂id
   */
  const getReceiptAddress = async (id: string) => {
    getReceiptAddressLoading.value = true
    const res = await getCustomerReceiptAddress(id)
    receiptAddress.value = res.data.data.row
    getReceiptAddressLoading.value = false
  }

  return {
    receiptAddress,
    getReceiptAddressLoading,
    getReceiptAddress,
  }
}

/**
 * 确认公司签收
 */
export const useCustomerReceiptConfirm = () => {
  // 确认加载
  const confirmLoading = ref(false)

  /**
   * 确认签收
   * @param {String} id - 返厂id
   * @param {Array<string>} goodIds - 返厂产品id
   */
  const confirm = async (id: string, goodIds: Array<string>) => {
    confirmLoading.value = true
    const res = await customerReceiptConfirm(id, goodIds)
    confirmLoading.value = false
    return res.data
  }

  return {
    confirmLoading,
    confirm,
  }
}

/**
 * 退回物流填写
 */
export const useReturnLogisticsFill = () => {
  // 填写加载
  const fillLoading = ref(false)

  // 物流填写请求参数
  const fillParam = ref<ReturnFactoryAPIRequest.ReturnLogisticsFillParam>({
    receiveAddress: '',
    receiveArea: '',
    receiveCity: '',
    receivePhone: '',
    receiveProvince: '',
    receiveUsername: '',
    repairGoodsLogisticsVos: [],
  })

  /**
   * 填写方法
   * @param {String} id - 返厂id
   */
  const fill = async (id: string) => {
    fillLoading.value = true
    const res = await returnLogisticsFill(id, fillParam.value)
    fillLoading.value = false
    return res.data
  }

  return {
    fillParam,
    fillLoading,
    fill,
  }
}

/**
 * 获取收货地址
 */
export const useGetReceiptAddress = () => {
  // 获取地址加载
  const getAddressLoading = ref(false)

  // 收货地址
  const address = ref<ReturnFactoryReceiptAddress>({
    factoryReceiveAddress: '',
    factoryReceiveArea: '',
    factoryReceiveCity: '',
    factoryReceivePhone: '',
    factoryReceiveProvince: '',
    factoryReceiveUsername: '',
  })

  /**
   * 获取地址
   * @param id - 返厂id
   */
  const getAddress = async (id: string) => {
    getAddressLoading.value = true
    const res = await getReceiptAddress(id)
    address.value = res.data.data.row
    getAddressLoading.value = false
  }

  return {
    address,
    getAddressLoading,
    getAddress,
  }
}
